package com.htfyun.hardware;


import android.hardware.htfyun.INVRomDataService;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.htfyun.hardware.reflect.Utils;


//import android.os.ServiceManager;


public class NVRomController {

    private static String TAG = "NVRomController";

    private static NVRomController sInstance;
    private INVRomDataService mService = null;


    private NVRomController() {
//        IBinder binder = ServiceManager.getService("NVRomDataService");
        IBinder binder = Utils.getSystemServiceReflect("NVRomDataService");
        if (binder == null) {
            Log.e(TAG, "could not retrieve NVRomDataService");
            return;
        }
        Log.e(TAG, "NvDataTool binder = " + binder);
        mService = INVRomDataService.Stub.asInterface(binder);
    }

    public static synchronized NVRomController getInstance() {
        if (sInstance == null) {
            sInstance = new NVRomController();
        }
        return sInstance;
    }

    public String getAllIntegerInfoFromNVRomData(int startOffset, int count) {
        if (mService == null) {
            return "";
        }
        try {
            return mService.getAllIntegerInfo(startOffset, count);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return "";
    }

    public int getCountOfAllIntegerInfo() {
        if (mService == null) {
            return 0;
        }
        try {
            return mService.getCountOfAllIntegerInfo();
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return 0;
    }

    public String getAllStringInfoFromNVRomData(int startOffset, int count) {
        if (mService == null) {
            return "";
        }
        try {
            return mService.getAllStringInfo(startOffset, count);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return "";
    }

    public int getCountOfAllStringInfo() {
        if (mService == null) {
            return 0;
        }
        try {
            return mService.getCountOfAllStringInfo();
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return 0;
    }

    public boolean removeIntegerFromNVRomData(String key) {
        if (mService == null) {
            return false;
        }
        try {
            return mService.removeIntegerNVRomData(key);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return false;
    }

    public boolean removeStringFromNVRomData(String key) {
        if (mService == null) {
            return false;
        }
        try {
            return mService.removeStringNVRomData(key);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return false;
    }

    public int getNVRomInt(String key, int defValue) {
        if (mService == null) {
            return 0;
        }
        if (TextUtils.isEmpty(key)) {
            return defValue;
        }

        try {
            int value = mService.getNVRomInt(key, defValue);
            Log.d(TAG, "getNVRomInt:" + key + ",value:" + value);
            return value;
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return defValue;
    }

    public boolean setNVRomInt(String key, int value) {
        if (mService == null) {
            return false;
        }
        if (TextUtils.isEmpty(key)) {
            return false;
        }
        try {
            Log.d(TAG, "setNVRomInt:" + key + ",value:" + value);
            return mService.setNVRomInt(key, value);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return false;

    }


    public boolean setNVRomString(String key, String str) {
        if (mService == null) {
            return false;
        }
        if (TextUtils.isEmpty(key)) {
            return false;
        }

        try {
            Log.d(TAG, "setNVRomString:" + key + ",value:" + str);
            return mService.setNVRomString(key, str);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return false;
    }

    public String getNVRomString(String key) {
        if (mService == null) {
            return "";
        }
        if (TextUtils.isEmpty(key)) {
            return "";
        }
        try {
            String value = this.mService.getNVRomString(key);
            Log.d(TAG, "getNVRomString:" + key + ",value:" + value);
            return value;
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return "";
    }


}
